﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MvcShop.MVC.Extension
{
    public class RandomStrings
    {
        Random m_rnd = new Random();
        private char getRandomChar()
        {
            int ret = m_rnd.Next(122);
            while (ret < 48 || (ret > 57 && ret < 65) || (ret > 90 && ret < 97))
            {
                ret = m_rnd.Next(122);
            }
            return (char)ret;
        }
        public string getRandomString(int length)
        {
            StringBuilder sb = new StringBuilder(length);
            for (int i = 0; i < length; i++)
            {
                sb.Append(getRandomChar());
            }
            return sb.ToString();
        }

        internal string SelectRandomWord(int numberOfChars)
        {
            if (numberOfChars > 36)
            {
                throw new InvalidOperationException("随机字符不能超过36位。");
            }
            char[] columns = new char[36];

            for (int charPos = 65; charPos < 65 + 26; charPos++)
                columns[charPos - 65] = (char)charPos;

            for (int intPos = 48; intPos <= 57; intPos++)
                columns[26 + (intPos - 48)] = (char)intPos;

            StringBuilder randomBuilder = new StringBuilder();
            for (int incr = 0; incr < numberOfChars; incr++)
            {
                randomBuilder.Append(columns[m_rnd.Next(36)].ToString());
            }
            return randomBuilder.ToString();
        }
    }
}
